//                       .::::.
//                     .::::::::.
//                    :::::::::::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                ..::::::::::::.
//              ``::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ```` ':.          ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
package com.xw.service;

import com.github.pagehelper.PageHelper;
import com.xw.dao.PlayRecordMapper;
import com.xw.dao.PlayRecordSonMapper;
import com.xw.dao.UserMapper;
import com.xw.model.PlayRecord;
import com.xw.model.PlayRecordSon;
import com.xw.model.User;
import com.xw.util.AppUtil;
import com.xw.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class PlayRecordSonServiceImpl implements PlayRecordSonService {
    @Resource
    private PlayRecordSonMapper playRecordSonMapper;
    @Resource
    private PlayRecordMapper playRecordMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public Map findNearVideoItemId(String userId, String videosId) {
        //验证是否拉黑
        if (!StringUtils.equals("-1",userId) && StringUtils.isNotBlank(userId)) {
            User user = userMapper.selectByPrimaryKey(userId);
            if (user.getUserStatus() == 2) {
                return JsonData.returnMap(811, "您的账号已被锁定");
            }
        }

        PageHelper.startPage(1,1,false);
        PlayRecordSon playRecordSon =  playRecordSonMapper.findNearVideoItemId(userId,videosId);
        return JsonData.success(playRecordSon);
    }

    @Override
    public Map savePlayRecordSon(String userId, String videosId, String videoItemId, Double videoItemSize) {
        //验证是否拉黑
        if (!StringUtils.equals("-1",userId) && StringUtils.isNotBlank(userId)) {
            User user = userMapper.selectByPrimaryKey(userId);
            if (user.getUserStatus() == 2) {
                return JsonData.returnMap(811, "您的账号已被锁定");
            }
        }

         PlayRecordSon model = new PlayRecordSon();
         model.setVideosId(videosId);
         model.setVideoItemId(videoItemId);
         model.setUserId(userId);
         PlayRecordSon playRecordSon = playRecordSonMapper.selectOne(model);
         int i = 0;
         if (playRecordSon != null){
             playRecordSon.setVideosId(videosId);
             playRecordSon.setVideoItemId(videoItemId);
             playRecordSon.setUserId(userId);
             playRecordSon.setVideoItemSize(videoItemSize);
             playRecordSon.setCreateTime(new Date());
             playRecordSonMapper.updateByPrimaryKeySelective(playRecordSon);
         }else{
             playRecordSon = new PlayRecordSon();
             playRecordSon.setId(AppUtil.uuid());
             playRecordSon.setVideosId(videosId);
             playRecordSon.setUserId(userId);
             playRecordSon.setVideoItemId(videoItemId);
             playRecordSon.setVideoItemSize(videoItemSize);
             playRecordSon.setCreateTime(new Date());
             playRecordSonMapper.insertSelective(playRecordSon);
         }

         PlayRecord model2 = new PlayRecord();
         model2.setVideosId(videosId);
         model2.setUserId(userId);
         PlayRecord playRecord = playRecordMapper.selectOne(model2);

         if (playRecord != null){
             playRecord.setCreateTime(new Date());
             i = playRecordMapper.updateByPrimaryKeySelective(playRecord);
         }else{
             playRecord = new PlayRecord();
             playRecord.setId(AppUtil.uuid());
             playRecord.setVideosId(videosId);
             playRecord.setUserId(userId);
             playRecord.setCreateTime(new Date());
             i = playRecordMapper.insertSelective(playRecord);
         }

         return JsonData.returnMap(i);
    }
}
